package frame;

import org.junit.Test;

import java.io.*;
import java.util.Base64;

public class Account {
	/**Rc4密钥*/
	private static final String RC4_KEY = "";
	/**文件路径*/
	private static final String PATH = "F:/doc/";
	/**源文件*/
	private static final String RESOURCE = "accout.txt";
	/**加密文件*/
	private static final String ENCRYPTION = "encryption";
	/**解密文件*/
	private static final String DECRYPTION = "accout1.txt";

	/**
	 * 文件加密<br>
	 * 1. 读取源文件<br>
	 * 2. rc4加密<br>
	 * 3. 进行base64编码<br>
	 * 4. 将编码的字符写入磁盘
	 */
	//@Test
	public void encode() {
		String str = read(PATH+RESOURCE);
		str = codeRc4(str, RC4_KEY);
		str = encode64(str);
		System.out.println(str);
		writer(str,PATH+ENCRYPTION);
	}
	/**
	 * 文件解密<br>
	 * 1. 读取加密文件<br>
	 * 2. 进行base64解码<br>
	 * 3. rc4解密<br>
	 * 4. 将解密后的字符写入磁盘
	 */
	//@Test
	public void decode() {
		String str = read(PATH+ENCRYPTION);
		str = decode64(str);
		str = codeRc4(str, RC4_KEY);
		System.out.println(str);
		writer(str,PATH+DECRYPTION);
	}
	//读文件
	public static String read(String fileName) {
		FileInputStream in = null;
		StringBuffer buff = new StringBuffer();
		try {
			in = new FileInputStream(new File(fileName));
			byte []cbuf = new byte[1024];
			int len;
			while((len = in.read(cbuf))!=-1){
				buff.append(new String(cbuf,0,len));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(in);
		}
		return buff.toString();
	}
	//写文件
	public static void writer(String context,String fileName) {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(new File(fileName));
			out.write(context.getBytes());

			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			close(out);
		}
	}
	private static void close(Closeable closer) {
		if (closer == null ) return;
		try {
			closer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	 // 编码
    public static String encode64(String str) {
    	if (str==null||"".equals(str.trim())) {
			return "";
		}
        byte[] b = null;
        String s = null;
        try {
            b = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (b != null) {
            s = Base64.getEncoder().encodeToString(b);
        }
        return s;
    }

    // 解码
    public static String decode64(String str) {
    	if (str==null||"".equals(str.trim())) {
			return "";
		}
        byte[] b = null;
        String s = null;
        try {
            b = Base64.getDecoder().decode(str);
            s = new String(b, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return s;
    }
    //RC4加解密
    public static String codeRc4(String val,String key) {
        int[] iS = new int[256];
        byte[] iK = new byte[256];

        for (int i=0;i<256;i++)
            iS[i]=i;

        int j = 1;

        for (short i= 0;i<256;i++)
        {
            iK[i]=(byte)key.charAt((i % key.length()));
        }

        j=0;
        for (int i=0;i<255;i++)
        {
            j=(j+iS[i]+iK[i]) % 256;
            int temp = iS[i];
            iS[i]=iS[j];
            iS[j]=temp;
        }

        int i=0;
        j=0;
        char[] iInputChar = val.toCharArray();
        char[] iOutputChar = new char[iInputChar.length];
        for(short x = 0;x<iInputChar.length;x++)
        {
            i = (i+1) % 256;
            j = (j+iS[i]) % 256;
            int temp = iS[i];
            iS[i]=iS[j];
            iS[j]=temp;
            int t = (iS[i]+(iS[j] % 256)) % 256;
            int iY = iS[t];
            char iCY = (char)iY;
            iOutputChar[x] =(char)( iInputChar[x] ^ iCY) ;
        }
        return new String(iOutputChar);
    }

}
